package cn.edu.hqu.cst.zwl.aidldemoclient;

import androidx.appcompat.app.AppCompatActivity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import cn.edu.hqu.cst.zwl.aidldemo.ICat;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private ICat catService;
    private Button btnGetRemoteServiceStatus;
    private TextView tvRemoteServiceColor,tvRemoteServiceWeight;
    private ServiceConnection conn=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            catService=ICat.Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            catService=null;

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnGetRemoteServiceStatus=findViewById(R.id.btn_main_activity_get_remote_service_status);
        tvRemoteServiceColor=findViewById(R.id.tv_main_activity_show_color);
        tvRemoteServiceWeight=findViewById(R.id.tv_main_activity_show_weight);
        btnGetRemoteServiceStatus.setOnClickListener(this);
        Intent intent=new Intent();
        intent.setAction("cn.edu.hqu.cst.zwl.action.AIDL_SERVICE");
        intent.setPackage("cn.edu.hqu.cst.zwl.aidldemo");
        bindService(intent,conn,Service.BIND_AUTO_CREATE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_main_activity_get_remote_service_status:
                try {
                    tvRemoteServiceColor.setText("跨进程服务提供的颜色为："+catService.getColor());
                    tvRemoteServiceWeight.setText("跨进程服务提供的重量为："+catService.getWeight());
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        this.unbindService(conn);
    }
}